<?php
/**
 * @version $Id: multifilemail.class.php 21 2010-05-14 13:24:08Z vladzimirs $
 * @package UnoCart
 * @copyright Авторские права (C) 2009-2010 UnoCart team & (C) 2007-2010 Joostina team &. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * UnoCart! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */
defined('_VALID_MOS') or die('Restricted access');
/*
$mail = new multiFileMail($from, $to, $subject, $message);
$mail->sendMail();
*/
class multiFileMail
{
    var $from;
    var $fromname;
    var $parts;
    var $to;
    var $headers;
    var $body;

    function multiFileMail($from, $fromname, $to, $subject, $message, $charset =
        'iso-8859-1')
    {
        $this->from = $from;
        $this->fromname = $fromname;
        $this->to = $to;
        $this->subject = $subject;
        $this->body = $message;
        $this->charset = $charset;
        $this->headers = array();
    }

    function attachFile($file_name, $file_content = "", $mime_type = "")
    {
        if (!$file_name) {
            // Main body
            $this->headers[] = array("name" => $file_name, "content" => $file_content,
                "mimetype" => $mime_type);
            return;
        }

        $file_content = file_get_contents($file_name);
        require_once (JPATH_BASE . "/components/com_unocart/mail/mimetypes.class.php");
        $abs_lib_path = dirname(__file__);
        $check_filename = 'mimetypes.rar';
        $mime = new MimeTypes($abs_lib_path);
        $mime_type = $mime->getFileMime(basename($file_name), 1); // Vsi mime-types aray
        $this->headers[] = array("name" => basename($file_name), "content" => $file_content,
            "mimetype" => $mime_type);
    }
    function getFileName($filename)
    {
        $filename = basename($filename);
        return substr($filename, 0, Jstring::strrpos($filename, "."));
    }

    function parseBody()
    {
        $pattern = '(<img [0-9a-zA-Z=_:./": -]*/?>)';
        preg_match_all($pattern, $this->body, $images);
        if (count($images[0])) {
            foreach ($images[0] as $key => $value) {
                $pattern = '(src = "([0-9a-zA-Z=_:./": -]*)")';
                preg_match_all($pattern, $value, $src);
                $this->attachFile($src[1][0]);
                $searches[$src[1][0]] = "cid:" . $this->getFileName($src[1][0]);
            }
            $this->body = strtr($this->body, $searches);
        }
        return;
    }

    function buildLetter($header)
    {
        $letter = $header["content"];
        if ($header["mimetype"] != "text/plain" && $header["mimetype"] != "text/html") {
            $letter = chunk_split(base64_encode($letter));
            $encoding = "base64";
            $additional = "\nContent-ID: <" . $this->getFileName($header['name']) . ">\nContent-Disposition: inline; filename=\"$header[name]\"\n";
        } else {
            $header["mimetype"] .= '; charset="' . $this->charset . '"';
            $encoding = $header["mimetype"];
            $encoding = "8bit";
            $additional = '';
        }
        return "Content-Type: " . $header["mimetype"] . (($header["name"]) ?
            '; name = "' . $header["name"] . '"' : "") . "\nContent-Transfer-Encoding: " . $encoding .
            $additional . "\n\n" . $letter . "\n";
    }

    function setMultipartMail()
    {
        $multipart = "Content-Type: multipart/related; \n    type = \"text/html\";\n    boundary=\"$this->boundary\"\n\n";
        $count = count($this->headers);
        for ($i = $count - 1; $i >= 0; $i--) {
            $multipart .= "\n" . "--$this->boundary\n" . $this->buildLetter($this->headers[$i]);
        }
        return $multipart .= "--$this->boundary--";
    }

    function getFullMessage()
    {
        $text = "";
        $this->parseBody();
        if (!empty($this->from)) {
            $text .= "From: " . $this->fromname . " <" . $this->from . ">" . " \n";
        }
        $this->attachFile("", $this->body, "text/html");
        if (!empty($this->body)) {
            $this->boundary = "b" . md5(uniqid(time()));
            $text .= "MIME-Version: 1.0\n" . $this->setMultipartMail();
        }
        return $text;
    }

    function sendMail()
    {
        $headers = $this->getFullMessage();
        mail($this->to, $this->subject, "", $headers);
    }
}

?>